package com.mach.platform.sprider.dev;


import com.mach.platform.sprider.getter.DataCenter;
import com.mach.platform.util.SqlHelper;

import java.io.IOException;

/*
    LinkKeWork S7244
 */
public class LinkWalkDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;

    public LinkWalkDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls = DataCenter.sqlHelper;

        this.devName = devName;
    }

    public static void main(String[] args) throws IOException, InterruptedException {


        //setId(args);//设置

//        openFak(args);
//        Thread.sleep(10*1000);
//        closeFak(args);

        //openLengQueTa("192.168.1.200",30061,2,"XXXX" );
//        openLengQueTa("192.168.1.200",30061,5, "XXXX" );
//        Thread.sleep(10*1000);
//        closeLengQueTa("192.168.1.200",30017,6,"XXXX" );

//        openLengQueTa("192.168.1.200",30061,2, "XXXX" );
//        closeLengQueTa("192.168.1.200",30061,2,"XXXX" );

//        LinkWalkDev dev =  new LinkWalkDev( "192.168.1.200",30053,7,"XXXX" );

//        showValue(dev);

        //startMain();
        if(true){
            findAll(args);//扫描
        }

    }
    public static void startMain() throws IOException {
        LinkWalkDev dev =  new LinkWalkDev("192.168.1.200",30017,10, "");
        dev.setDataRtu(2554, 0, 6);
    }


    public static void findAll(String[] args) throws IOException {
        int busNumStartIx = 2,busNumEndIx = 2; //总线地址开始与结束参数
        int tcpPortStartIx = 30017,tcpPortEndIx = 30017; //TCP端口开始与结束参数
        String ip = "192.168.1.200";
        for(int j = tcpPortStartIx; j <= tcpPortEndIx; j+=4){
            for(int i = busNumStartIx; i <= busNumEndIx; ++i){
                for(int k = 4001; k <= 4001; k+=1){
                    Object result = null;
                    byte[] databuf = readIdById(ip, j, i, "XXXX", k);
//                    databuf = ByteUtil.subData(databuf,3,2);
//                    databuf = ByteUtil.reverse(databuf);
//                    result = ByteUtil.getShort(databuf);
//                    System.out.println("result="+ result);
                }
            }
        }
    }
    public static void showValue(LinkWalkDev dev){

        System.out.println(" di=" + dev.getDI(100));
        System.out.println(" di=" + dev.getDI(101));
        System.out.println(" di=" + dev.getDI(102));
        System.out.println(" di=" + dev.getDI(103));

        System.out.println();

        System.out.println(" di=" + dev.getDI(108));
        System.out.println(" di=" + dev.getDI(109));
    }


    public static void openLengQueTa(String ip, Integer port,Integer devId, String devName) throws IOException {
        LinkWalkDev dev =  new LinkWalkDev( ip,  port, devId,  devName );
        Short di1 = dev.getDI(100);
        Short di2 = dev.getDI(101);
        Short di3 = dev.getDI(102);
        Short do1 = dev.getDI(108);
        System.out.println("start di1=" + di1);
        System.out.println("start di2=" + di2);
        System.out.println("start di3=" + di3);
        System.out.println(" do1=" + do1);

        byte[] data2 = dev.setDataRtu(108,1);
        UU.printData(data2);

        di1 = dev.getDI(100);
        di2 = dev.getDI(101);
        di3 = dev.getDI(102);
        do1 = dev.getDI(108);

        System.out.println(" di1=" + di1);
        System.out.println(" di2=" + di2);
        System.out.println(" di3=" + di3);
        System.out.println(" do1=" + do1);
    }

    public static void closeLengQueTa(String ip, Integer port,Integer devId, String devName) throws IOException {
        LinkWalkDev dev =  new LinkWalkDev( ip,  port, devId,  devName );
        Short di1 = dev.getDI(100);
        Short di2 = dev.getDI(101);
        Short di3 = dev.getDI(102);
        System.out.println("start di1=" + di1);
        System.out.println("start di2=" + di2);
        System.out.println("start di3=" + di3);

        byte[] data2 = dev.setDataRtu(108,0);
        UU.printData(data2);

        di1 = dev.getDI(100);
        di2 = dev.getDI(101);
        di3 = dev.getDI(102);

        System.out.println(" di1=" + di1);
        System.out.println(" di2s=" + di3);
        System.out.println(" di3=" + di3);
    }


    public static void openFak(String[] args) throws IOException {
        LinkWalkDev dev =  new LinkWalkDev("192.168.1.200",30017,6,"XXXX" );
        Short di1 = dev.getDI(100);
        Short di3 = dev.getDI(102);
        System.out.println("start di1=" + di1);
        System.out.println("start di3=" + di3);

        byte[] data2 = dev.setDataRtu(108,1);
        UU.printData(data2);
        long openTimeOut = 40;
        long st = System.currentTimeMillis();
        boolean isReadNextDI1 = true;
        while(isReadNextDI1){
            long cur =  System.currentTimeMillis();

             di1 = dev.getDI(100);
             di3 = dev.getDI(102);

            System.out.println(" di1=" + di1);
            System.out.println(" di3=" + di3);

            if(((cur - st)/1000) > openTimeOut){
                byte[] data3 = dev.setDataRtu(108,0);
                UU.printData(data2);
                isReadNextDI1 = false;
            }

            if(di3 > 0){
                byte[] data3 = dev.setDataRtu(108,0);
                isReadNextDI1 = false;
            }

        }
        di1 = dev.getDI(100);
        di3 = dev.getDI(102);

        System.out.println(" di1=" + di1);
        System.out.println(" di3=" + di3);
    }

    public static void closeFak(String[] args) throws IOException {
        LinkWalkDev dev =  new LinkWalkDev("192.168.1.200",30017,6,"XXXX" );
        Short di1 = dev.getDI(101);
        Short di3 = dev.getDI(103);
        System.out.println("start di1=" + di1);
        System.out.println("start di3=" + di3);

        byte[] data2 = dev.setDataRtu(109,1);
        UU.printData(data2);
        long openTimeOut = 40;
        long st = System.currentTimeMillis();
        boolean isReadNextDI1 = true;
        while(isReadNextDI1){
            long cur =  System.currentTimeMillis();

            di1 = dev.getDI(101);
            di3 = dev.getDI(103);

            System.out.println(" di2=" + di1);
            System.out.println(" di4=" + di3);

            if(((cur - st)/1000) > openTimeOut){
                byte[] data3 = dev.setDataRtu(109,0);
                UU.printData(data2);
                isReadNextDI1 = false;
            }

            if(di3 > 0){
                byte[] data3 = dev.setDataRtu(109,0);
                isReadNextDI1 = false;
            }

        }
        di1 = dev.getDI(101);
        di3 = dev.getDI(103);

        System.out.println(" di2=" + di1);
        System.out.println(" di4=" + di3);
    }

    public static void setId(String[] args) throws IOException {
        LinkWalkDev dev =  new LinkWalkDev("192.168.1.200",30029,10,"XXXX" );
        byte[] data = dev.readSth(10, 1, 3);
        UU.printData(data);
        byte[] data2 = dev.setDataRtu(10,11);
        UU.printData(data2);
    }
    public static byte[] readIdById(String ip , Integer port, Integer id, String devName, Integer addr) throws IOException {
        LinkWalkDev dev =  new LinkWalkDev(ip,port,id,devName);
        byte[] data = dev.readSth(addr, 2, 3);
//        UU.printData(data);
        return data;
    }

    public Short getDI(Integer addr) {
        Short result = null;

        try {
            byte[] repdata = this.getDataRtu(addr,1);
            byte[] data = ByteUtil.subData(repdata,3,2);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getShort(data);

        } catch (Exception e) {
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    public byte[] readSth(Integer addr, Integer num, Integer cmdCode){
        byte[] result = null;


        byte[] repData = new byte[0];
        try {
            repData = this .getDataRtu(addr,num);
//            if(repData.length <1)return null;
//            byte[] data = ByteUtil.subData(repData,3,4);
//            data = ByteUtil.reverse(data);
            result = repData;
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }





    @Override
    public void run(){

//        long preSec = 0;
//        long nowSec = 0;
//        while(true){
//
//
//            try {
//
//                DateTime dateTime = new DateTime();
//                int fyear = dateTime.getYear();
//                int fmonth = dateTime.getMonthOfYear();
//                int fday = dateTime.getDayOfMonth();
//                int fhour = dateTime.getHourOfDay();
//                int fmin = dateTime.getMinuteOfHour();
//                int fsec = dateTime.getSecondOfMinute();
//
//                Float volt =  this.getVolt();
//                Float elect = this.getElect();
//                Float power = this.getPower();
//                Double enery = this.getEnery();
//
//                try {
//                    this.sqls.updateAsync("insert ignore into data_detail(dev_id, fyear, fmonth, fday, fhour, fmin, fsec,fdatetime, " +
//                            "volt,elect,power,enery) value" +
//                            "('"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
//                            "'%Y-%m-%d %H:%i:%s'),"
//                            +volt+","+elect+","+power+","+enery+")");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                try {
//                    this.sqls.updateAsync("replace into data_cur (dev_id,volt,elect,power,update_time) value " +
//                            "( " +
//                            "'"+this.devName+"',"+
//                            volt + ","+
//                            elect + ","+
//                            power + "," +
//                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
//                                    "'%Y-%m-%d %H:%i:%s')" +
//                            ")");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
////                Thread.sleep(1000*60);
//
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            preSec = nowSec;
//
//
//        }


    }

    /*
      取变频器:电压
   */
    public Float getVolt() {
        Float result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40006,4);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getFloat(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }



    /*
    取变频器:能量
 */
    public Double getEnery() {
        Double result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40266,8);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,8);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getDouble(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

}
